﻿using System.Collections.Generic;
using Dawn.SingleTon;
using FairyGUI;

namespace GameMain.Scripts.UI.Framework
{
    public class UIMgr : MonoBehaviour
    {
        private static UIMgr _In;

        //UI框架使用 标准屏幕长宽
        public const int StandardScreenWidth = 1920;
        public const int StandardScreenHeight = 1080;
        
        public static float GlobalFitScale { get; private set; }
        public static float WorldFullScale { get; private set; }//全屏特效用
        public static float SafeWidth { get; private set; }

        private readonly List<UIPage> _pageList = new List<UIPage>();
        
        private void Awake()
        {
            _In = this;
            GRoot.inst.SetContentScaleFactor(StandardScreenWidth, StandardScreenHeight,
                UIContentScaler.ScreenMatchMode.MatchWidthOrHeight);
            GlobalFitScale = UIUtils.GetFitScale();
            WorldFullScale = UIUtils.GetFullScreenScale(StandardScreenWidth, StandardScreenHeight) * GlobalFitScale;
            SafeWidth = UIUtils.GetSafeWidth();
        } 

        internal static UIPage AddPage(UIPage page)
        {
            _In._pageList.Add(page);
            return page;
        }
        
        //把战斗Page移到最前面来 提高性能
        public static void VipPage<T>()
        {
            var list = _In._pageList;
            for (var i = list.Count - 1; i >= 1; i--)
            {
                if (list[i] is T)
                {
                    (list[i], list[0]) = (list[0], list[i]);
                    return;
                }
            }
        }
        
        public static T GetPage<T>() where T : UIPage
        {
            for (int i = 0; i < _Me._pageList.Count; i++)
            {
                if (_In._pageList[i] is T page)
                    return page;
            }
            return default;
        }
        
        public static UIPage GetCurrentPage()
        {
            for (var i = _Me._pageList.Count - 1; i >= 0; i--)
            {
                if (_In._pageList[i].View.visible)
                    return _Me._pageList[i];
            }
            return default;
        }
        
        internal static UIPage RemovePage(UIPage page)
        {
            _In._pageList.Remove(page);
            return page;
        }
    }
}

